package com.webex.util;

import java.awt.geom.Point2D;

import org.robochina.airobot.tank.Bot;
import org.robochina.math.MathUtils;

import com.webex.mirage.MirageBaseRobot;
import com.webex.mirage.MirageRobot;

public class PatternMatchFire {
	//����Ļ���
	private static final double POWER = 0.5;
	
	//������ʷ��¼����󳤶�
	private static final int HISTORY_LENGHT = 2000;
	//����ƥ��εĳ���
	private static final int MATCH_LENGHT = 20;
	//���ֵ��ٶȼ�¼
	private double[] velocityRecord = new double[HISTORY_LENGHT];
	//���ֵķ����¼
	private double[] headingRecord = new double[HISTORY_LENGHT];
	//����ĵ�ǰ����
	private int currentIndex=0;
	
	//��¼��ǰ�Ļ�����״̬
	public void record(Bot bot)
	{
		velocityRecord[currentIndex] = bot.getVelocity();
		headingRecord[currentIndex] = bot.getHeading();
		currentIndex++;
	}
	
	//������ѵ�ƥ���
	public int getMatchIndex()
	{
		double beatSimilarity=Double.POSITIVE_INFINITY;
		int matchIndex=0;
		//����ȡi<currentFrame-100��Ϊ�˱���Ƚ���ͱ��Ƚ����ظ�
		//����ȡ�㹻�Ľڵ�����δ�������
		for(int i=MATCH_LENGHT; i<currentIndex-MATCH_LENGHT; i++)
		{
			//ȡ10����ڵ�������ƶ�
			double similarity=0;
			for(int j=1; j<=MATCH_LENGHT; j++)
			{
				similarity+=Math.abs(velocityRecord[i-j]-velocityRecord[currentIndex-j]);
				similarity+=Math.abs(headingRecord[i-j]-headingRecord[currentIndex-j]);
				//����Ȩֵ��ƥ��ȼ���
				//similarity+=Math.abs(velocityRecord[i-j]-velocityRecord[currentIndex-j])/8;
				//similarity+=Math.abs(headingRecord[i-j]-headingRecord[currentIndex-j])/Math.PI;
			}
			//��¼�����Ƶ����ƶȣ��Լ���Ӧ�ļ�¼�ڵ��±�
		   	if(similarity<beatSimilarity)
		   	{
			    matchIndex=i;
			   	beatSimilarity=similarity;
    		}
		}
		return matchIndex;
	}
	
//	//�õ������λ��
//	private Point2D getFirePoint(double power)
//	{
//		int matchIndex  = getMatchIndex();
//		//Ԥ��λ��
//        Point2D firePoint = new MirageRobot().getFirstOpponent().getLocation();
//        int time = 0;
//        while(matchIndex+time<currentIndex)
//        {
//        	double distance = MathUtils.distance(new MirageRobot().getLocation(), firePoint);
//        	if(distance/new MirageRobot().getBulletVelocity(power)<=time) break;
//        	firePoint = MathUtils.nextPoint(firePoint, 
//        		headingRecord[matchIndex+time], velocityRecord[matchIndex+time]);
//        	time++;
//        }
//		return firePoint;
//	}
	
	public Point2D getFirePoint(MirageRobot self, double power) {
		// Ԥ��λ��
		int matchIndex  = getMatchIndex();
		Point2D firePoint = self.getTarget().getLocation();
		int time = 0;
		while (matchIndex + time < currentIndex) {
			double distance = MathUtils.distance(self.getLocation(), firePoint);
			if (distance / self.getBulletVelocity(power) <= time)
				break;
			firePoint = MathUtils.nextPoint(firePoint, headingRecord[matchIndex
					+ time], velocityRecord[matchIndex + time]);
			time++;
		}
		return firePoint;
	}
	
	public Point2D getFirePoint(MirageBaseRobot self, double power) {
		// Ԥ��λ��
		int matchIndex  = getMatchIndex();
		Point2D firePoint = self.getTarget().getLocation();
		int time = 0;
		while (matchIndex + time < currentIndex) {
			double distance = MathUtils.distance(self.getLocation(), firePoint);
			if (distance / self.getBulletVelocity(power) <= time)
				break;
			firePoint = MathUtils.nextPoint(firePoint, headingRecord[matchIndex
					+ time], velocityRecord[matchIndex + time]);
			time++;
		}
		return firePoint;
	}

}
